home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / simula / books / books.lha / kirkerud / quicksort.sim < prev    next >
Text File  |  1993-08-16  |  1KB  |  30 lines

  1. procedure Quicksort(table, low_bound, high_bound);
  2.     integer array table;  integer low_bound, high_bound;
  3.   !  Sorts the elements in table(low_bound : high_bound) in non-decreasing order;
  4. if low_bound < high_bound then
  5. begin integer some_value, last_below, last_equal,  first_above, ind, x;
  6.   some_value  := table(low_bound);
  7.   last_below  := low_bound - 1;
  8.   last_equal  := low_bound;
  9.   first_above := high_bound + 1;
  10.   ind         := low_bound + 1;
  11.   while ind < first_above do
  12.     begin
  13.       x := table(ind);      
  14.       if x < some_value then
  15.         begin
  16.           last_below := last_below + 1;     last_equal := last_equal + 1;
  17.           table(ind) := table(last_below);  table(last_below) := x;
  18.           ind := ind + 1;
  19.         end else
  20.       if x = some_value then
  21.         begin last_equal := last_equal + 1;  ind := ind + 1 end
  22.       else begin
  23.           first_above := first_above - 1;
  24.           table(ind) := table(first_above);  table(first_above) := x;
  25.         end;
  26.     end;
  27.   Quicksort(table,   low_bound, last_below);
  28.   Quicksort(table, first_above, high_bound);
  29. end of Quicksort;
  30.